package com.cool.common;

import lombok.Getter;

import java.io.Serializable;

/**
 * 共同的结果
 *
 * @author Jiangmanman
 * @date 2020/07/23
 */
@Getter
public class CommonResult implements Serializable {
    /**
     * 200 ok
     * 301 created
     * 401 unauthorized
     * 403 forbidden
     * 404 not found
     */
    public static final Integer SUCCESS = 200;
    public static final Integer FAILED = 500;
    public static final Integer LAYUI_CODE_RIGHT = 0;
    public static final Integer LAYUI_CODE_ERROR = -1;
    public static final Integer COUNT_NUMBER = 1;
    /**
     * Layui 默认 0,-1为错误
     */
    private Integer code = 0;
    /**
     * 状态码 默认 200
     */
    private Integer status;
    /**
     * 返回信息 默认 操作成功
     */
    private String msg;
    /**
     * 数据条数 默认 1
     */
    private Integer count;
    /**
     * 数据
     */
    private Object data;

    public static CommonResult newCommonResult(){

        return new CommonResult();
    }

    public CommonResult success(){
        this.code =  CommonResult.LAYUI_CODE_RIGHT;
        this.status = CommonResult.SUCCESS;
        this.msg = "操作成功";
        this.count = CommonResult.COUNT_NUMBER;
        this.data = "成功";
        return this;
    }
    public CommonResult success(Object data){
        this.code = CommonResult.LAYUI_CODE_RIGHT;
        this.status = CommonResult.SUCCESS;
        this.msg = "操作成功";
        this.count = CommonResult.COUNT_NUMBER;
        this.data = data;
        return this;
    }
    public CommonResult success(Integer code, Object data){
        this.code = code;
        this.status = CommonResult.SUCCESS;
        this.msg = "操作成功";
        this.count = CommonResult.COUNT_NUMBER;
        this.data = data;
        return this;
    }
    /**
     * 普通失败提示信息
     */
    public CommonResult failed() {
        this.code = CommonResult.LAYUI_CODE_ERROR;
        this.status = CommonResult.FAILED;
        this.msg = "操作失败";
        this.count = CommonResult.COUNT_NUMBER;
        this.data = "error";
        return this;
    }
    public CommonResult failed(Object data) {
        this.code = CommonResult.LAYUI_CODE_ERROR;
        this.status = CommonResult.FAILED;
        this.msg = "操作失败";
        this.count = CommonResult.COUNT_NUMBER;
        this.data = data;
        return this;
    }
    public CommonResult failed(String msg, Object data) {
        this.code = CommonResult.LAYUI_CODE_ERROR;
        this.status = CommonResult.FAILED;
        this.msg = msg;
        this.count = CommonResult.COUNT_NUMBER;
        this.data = data;
        return this;
    }

    public CommonResult failed(String message, Integer status, Integer count, Object data){
        this.code = CommonResult.LAYUI_CODE_ERROR;
        this.status = status;
        this.msg = "操作失败";
        this.count = count;
        this.data = data;
        return this;
    }

    public CommonResult setCode(Integer code) {
        this.code = code;
        return this;
    }

    public CommonResult setStatus(Integer status) {
        this.status = status;
        return this;
    }

    public CommonResult setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public CommonResult setCount(Integer count) {
        this.count = count;
        return this;
    }

    public CommonResult setData(Object data) {
        this.data = data;
        return this;
    }

}
